﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApi.Model;

namespace WebApi.Controllers
{
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // POST api/values
        [HttpPost("api/login")]
        public ApiResponseEntity Post([FromBody] LoginModel user)
        {
            if (user.UserID == "" || user.Pwd == "")
            {
                return new ApiResponseEntity().Error("参数不正确", false);
            }
            else
            {
                DataSet ds = DBHelper.Query("select * from users where username='" + user.UserID + "' and pwd='" + user.Pwd + "'");
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string user_item_id = ds.Tables[0].Rows[0]["itemid"].ToString();
                    string personName = ds.Tables[0].Rows[0]["userName"].ToString();
                    string address = ds.Tables[0].Rows[0]["address"].ToString();
                    return new ApiResponseEntity().Success("成功", new { addr = address, item_id = user_item_id, name = personName, code = 200 });
                }
                else
                {
                    return new ApiResponseEntity().Error("账号或密码错误", false);
                }

            }
        }


        // POST api/values
        [HttpPost("api/reg")]
        public ApiResponseEntity Reg([FromBody] RegModel reg)
        {
            if (reg.UserID == "" || reg.Pwd == "")
            {
                return new ApiResponseEntity().Error("参数不正确", false);
            }
            else
            {
                bool res = DBHelper.Exec($"insert into users(username,pwd,address) values('{reg.UserID}','{reg.Pwd}','{reg.Addr}')");
                if (res)
                {
                    return new ApiResponseEntity().Success("注册成功", true);
                }
                else
                {
                    return new ApiResponseEntity().Error("注册失败", false);
                }

            }
        }

        [HttpGet("api/getData")]
        public ApiResponseEntity GetData(string cardNo, string planId)
        {
            return new ApiResponseEntity().Success("成功", false);
        }
    }
}
